<?php
include_once 'root.php';
Class ConnectSap extends Root {
	public function __construct() {
		parent::__construct();
		
		$configSap = array(
		"logindata"=>array(
			/*"ASHOST"=>"/H/101.109.242.24/H/191.8.209.246"	// application server
			,"SYSNR"=>"02"					// system number
			,"CLIENT"=>"300"					// client
			,"USER"=>"kohkae"				// user
			,"PASSWD"=>"kohkae"				// password*/
			"ASHOST"=>"192.168.19.215"	// application server
			,"SYSNR"=>"00"					// system number
			,"CLIENT"=>"330"					// client
			,"USER"=>"winitap"				// user
			,"PASSWD"=>"alex1100"				// password
			,"LANG"=>"TH"					// language
			,"CODEPAGE"=>"8600"				// codepage 8600 for support thai language
		)
		,"show_errors"=>false					// let class printout errors
		,"debug"=>false) ;					// detailed debugging information

		$this -> load -> library("saprfc",$configSap);
		
	}

	public function index() {
		$this -> data['header'] = "connect sap";
		$this -> includeView("connectsap");
	}

	public function connect() {
		$this -> includeView("connectsap");
	}
	public function connectAction() {
		// load model
		$this -> load -> model("connectsapmodel");
		
		// connect sap and return to $result
		//$result = $this -> saprfc -> callFunction("ZSDRFC_CUSTOMER_DATA", array( array("TABLE", "CUSTOMER_DATA", array()), array("TABLE", "SALES_DATA", array())));
		$result = $this -> saprfc -> callFunction("SO_USER_LIST_READ",
								array(	array("IMPORT","USER_GENERIC_NAME","*"),
										array("TABLE","USER_DISPLAY_TAB",array())
								));
		// if start is ok will import data
		if ($this -> saprfc -> getStatus() == SAPRFC_OK) {
			// start import data send $result to function 
			$insert = $this -> connectsapmodel -> insertConnectData($result);
			//print_r $result;
			//exit();			
			// if saprfc OK show message in view page
			//$message = "SAP LOGIN OK";
			//$this -> set_res(true, $message);
			$this->saprfc->logoff();
			if ($insert['flag']) {
				$connectRow  = number_format($insert['connect_row']);
				//$saleRow = number_format($insert['sale_row']);
				$message = date("Y-m-d H:i:s")."<br />";
				$message.="TABLE: USER_DISPLAY_TAB added $connectRow RECORDS";
				$this -> set_res(true, $message);
			} else {
				$message = date("Y-m-d H:i:s")."<br />";
				$message.="TABLE: USER_DISPLAY_TAB added 0 RECORDS";
				$this -> set_res(false, $message);
			}
		} else {
			// if not conect sap
			$this->load->model("coremodel");
			// write log for can connect model
			$this->coremodel->insertLogFailed('USER_DISPLAY_TAB');
			//$this->coremodel->insertLogFailed('SALES_DATA');
			$resultMesage = $this -> saprfc -> getStatusText();
			$resultMesage = "";
			$this->saprfc->logoff();
			$message = date("Y-m-d H:i:s")."<br />";
			$message.="ERROR ON Connect SAP <br />TABLE: CUSTOMER_DATA added 0 RECORDS <br> TABLE: SALES_DATA added 0 RECORDS";
			$message.='<br /> with message '.$resultMesage;
			$this -> set_res(false, $message);
			
			
		}
		
	}

}
?>